﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Sunny.Entity;
using Sunny.Model.RequestParam;

namespace Sunny.Service
{
    public class DictDataService
    {
        private readonly SunnyContext _sunnyContext;
        private readonly UserService _userService;
        private readonly IMapper _mapper;

        public DictDataService(SunnyContext sunnyContext,UserService userService,IMapper mapper)
        {
            _sunnyContext = sunnyContext;
            _userService = userService;
            _mapper = mapper;
        }
        public async Task<List<SysDictData>> GetDictDataByType(string dictType)
        {
            var data = await _sunnyContext.SysDictData.Where(t => t.DictType == dictType)
                .OrderBy(t => t.DictSort).ToListAsync();
            return data;
        }

        public async Task<(List<SysDictData>, int)> List(DictDataListParam param)
        {
            var dictDatas  = _sunnyContext.SysDictData.Where(t=>t.DictType == param.DictType);
            if(!string.IsNullOrEmpty(param.DictLabel))
            {
                dictDatas = dictDatas.Where(t=>t.DictLabel.Contains(param.DictLabel));
            }
            if(!string.IsNullOrEmpty(param.Status))
            {
                dictDatas = dictDatas.Where(t=>t.Status == param.Status);
            }
            var total = await _sunnyContext.SysDictData.CountAsync();
            var dictDataList = await dictDatas.OrderBy(t=>t.DictSort).Skip((param.PageNum-1)*param.PageSize).Take(param.PageSize).ToListAsync();
            
            return (dictDataList,total);
        }

        public async Task<SysDictData> GetById(long dictCode)
        {
            return await _sunnyContext.SysDictData.SingleOrDefaultAsync(t=>t.DictCode == dictCode);
        }

        public async Task Update(DictDataEditParam param)
        {
            SysDictData sysDictData = await _sunnyContext.SysDictData.SingleOrDefaultAsync(t=>t.DictCode == param.DictCode);
            sysDictData.DictSort = param.DictSort;
            sysDictData.DictLabel = param.DictLabel;
            sysDictData.DictValue = param.DictValue;
            sysDictData.CssClass = param.CssClass;
            sysDictData.ListClass = param.ListClass;
            sysDictData.IsDefault = param.IsDefault;
            sysDictData.Status = param.Status;
            sysDictData.Remark = param.Remark;
            await _sunnyContext.SaveChangesAsync();
        }

        public async Task Delete(string dictCodes)
        {
            throw new System.NotImplementedException();
        }

        public async Task Add(DictDataEditParam param)
        {
            var currentUser = _userService.GetCurrentUser();
            SysDictData sysDictData = _mapper.Map<SysDictData>(param);
            await _sunnyContext.SysDictData.AddAsync(sysDictData);
            await _sunnyContext.SaveChangesAsync();
            
        }
    }
}